package tool

import "database/sql"

// NewMysqlConn 创建mysql链接
func NewMysqlConn() (db *sql.DB, err error) {
	db, err = sql.Open("mysql",
		"root:Alikai19940818@tcp(43.138.148.55:3306)/immoc?charset=utf8")
	return db, err
}

// GetResultRow 获取返回值
func GetResultRow(rows *sql.Rows) map[string]interface{} {
	columns, _ := rows.Columns()
	scanArgs := make([]interface{}, len(columns))
	values := make([]interface{}, len(columns))
	for i := range values {
		scanArgs[i] = &values[i]
	}
	record := make(map[string]interface{})
	for rows.Next() {
		// 将数据保存到record字典
		rows.Scan(scanArgs...)
		for i, v := range values {
			if v != nil {
				record[columns[i]] = string(v.([]byte))
			}
		}
	}
	return record
}
